package  
{
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Seven Gae
	 */
	public class Bola extends MovieClip
	{
		private var movingAngle:Number;
		public var velocidade : Number;
		
		public function Bola() 
		{
			this.x = Main.Self.stage.stageWidth / 2;
			this.y = Main.Self.stage.stageHeight / 2 + 200;
			Main.Self.screen.addChild(this);
			this.scaleX = this.scaleY = 0.3;
			movingAngle = 255;
			velocidade = 8;
		}
		
		public function update():void
		{
			verifyMovement();
			verifyCollisionWithScreenBounds();
			verifyRotation();
		}
		
		private function verifyMovement():void
		{
			this.x += Math.sin((movingAngle + 90) / 180 * Math.PI) * velocidade;
			this.y += Math.cos((movingAngle + 90) / 180 * Math.PI) * velocidade;	
		}
		
		private function verifyCollisionWithScreenBounds():void
		{
		
			var difAngle: Number;
			
			if (this.x < this.width / 2 )
			{
				difAngle =  movingAngle - 180;
				movingAngle -= (difAngle * 2) + 180;
				this.x = this.width / 2;
				//SoundManager.playSound("puff");
			}
			else if (this.x > Main.Self.stage.stageWidth - this.width / 2)
			{
				difAngle =  movingAngle - 0;
				movingAngle -= (difAngle * 2) + 180;
				this.x = Main.Self.stage.stageWidth - this.width / 2;
				//SoundManager.playSound("puff");
			}
			
			if (this.y < -50)
			{
				LevelManager.current = new YouWin();
			}
			else if (this.y > Main.Self.stage.stageHeight + 100)
			{
				LevelManager.current = new GameOver();
			}
		}
		
		private function verifyRotation():void 
		{
			this.rotation += velocidade;
		}
		
		public function destroy():void
		{
			Main.Self.screen.removeChild(this);
		}
		
		public function verifyCollisionWithNave(nave:Nave)
		{
			if (this.hitTestObject(nave) && nave.y > this.y)
			{
				
				this.y = nave.y - nave.height / 2 - this.height / 2;
				
				var angleRadian = Math.atan2(this.y - nave.y, this.x - nave.x);
				var angleDegree = angleRadian * 180 / Math.PI;
				
				if(angleDegree < 0)
				{
					angleDegree = (-angleDegree * -1) + 360;
				}		
				
				trace(angleDegree);
				
				movingAngle = -angleDegree;
				
				SoundManager.playSound("batida");
			}
		}
		
		public function verifyCollisionWithBrick(brickMap:Array):Boolean
		{
			for (var i: int = 0; i < brickMap.length; i++)
			{
				for (var j: int = 0; j < brickMap[0].length; j++)
				{
					if (brickMap[i][j] is MovieClip)
					{
						if (this.hitTestObject(brickMap[i][j]))
						{
							handleBrickCollision(brickMap[i][j], i, j, brickMap);
							return true;
						}
					}
				}
			}
			return false
		}
		
		private function handleBrickCollision(brick:MovieClip, i: int, j: int, brickMap:Array):void
        {
            var angleRadian: Number = Math.atan2(this.y - brick.y, this.x - brick.x);
            var angleDegree: Number = angleRadian * 180 / Math.PI;

            if (angleDegree < 0)
            {
                angleDegree = (-angleDegree * -1) + 360;
            }

			var difAngle: Number;

            if (angleDegree >= 225 && angleDegree <= 316)
            {
                difAngle =  movingAngle - 270;
				movingAngle -= (difAngle * 2) + 180;
				this.y = brick.y - brick.height / 2 - this.height / 2;
				if (brick is TijoloAntigo)
				{
					Particles.createParticles(10, brick.x, brick.y);
					Main.Self.screen.removeChild(brickMap[i][j]);
					brickMap[i][j] = 0;
					velocidade += 0.15;
					playDestroyBrickSound();
				}
				else
				{
					SoundManager.playSound("batida");
				}
            }
            //colisão pela esquerda
            else if (angleDegree >= 135 && angleDegree < 225)
            {
				difAngle =  movingAngle - 0;
				movingAngle -= (difAngle * 2) + 180;
				this.x = brick.x - brick.width / 2 - this.width; 
				if (brick is TijoloAntigo)
				{
					Particles.createParticles(10, brick.x, brick.y);
					Main.Self.screen.removeChild(brickMap[i][j]);
					brickMap[i][j] = 0;
					velocidade += 0.15;
					playDestroyBrickSound();
				}
				else
				{
					SoundManager.playSound("batida");
				}
            }
            //colisão pela direita
            else if ((angleDegree > 316 && angleDegree <= 360) || (angleDegree >= 0 && angleDegree <= 45))
            {
				difAngle =  movingAngle - 180;
				movingAngle -= (difAngle * 2) + 180;
				this.x = brick.x + brick.width / 2 + this.width; 
				if (brick is TijoloAntigo)
				{
					Particles.createParticles(10, brick.x, brick.y);
					Main.Self.screen.removeChild(brickMap[i][j]);
					brickMap[i][j] = 0;
					velocidade += 0.15;
					playDestroyBrickSound();
				}
				else 
				{
					SoundManager.playSound("batida");
				}
            }
            //colisão prr baixo
            else if (angleDegree > 45 && angleDegree < 135)
            {
                difAngle =  movingAngle - 90;
				movingAngle -= (difAngle * 2) + 180;
				this.y = brick.y + brick.height / 2 + this.height / 2;
				if (brick is TijoloAntigo)
				{
					Particles.createParticles(10, brick.x, brick.y);
					Main.Self.screen.removeChild(brickMap[i][j]);
					brickMap[i][j] = 0;
					velocidade += 0.15;
					playDestroyBrickSound();
				}
				else
				{
					SoundManager.playSound("batida");
				}
				
            } 
		
			
        }
		
		private function playDestroyBrickSound():void
		{
			var rn : int = Math.round(Math.random() * 3 + 1);
			
			SoundManager.playSound("toin" + rn.toString());
			
		}
    }

}